package services

import (
	"GOPAAS/internal/app/model"
)

type BomService struct {
	model.MrpBom
}

func GetBomService(pid int64) *BomService {
	db = getDB()
	var bom model.MrpBom
	if pid > 0 {
		db.First(&bom, pid)
	}
	return &BomService{bom}
}

// GetInfo 物料详情
func (p *BomService) GetInfo() (result BomItem, err error) {
	var data model.MrpBom
	query := db.Model(&model.MrpBom{})
	query.Preload("Company").Preload("Product").Preload("Product.ProductTmpl").Preload("ProductTmpl").Preload("BomLines").Preload("BomLines.ProductTmpl")
	query.Preload("Workcenters").Preload("Workcenters.Workcenter").Preload("Workcenters.Company")
	query.Preload("ByProducts").Preload("ByProducts.Product").Preload("ByProducts.Product.ProductTmpl").Preload("ByProducts.Operation")
	res := query.First(&data, p.Id)
	if res.Error != nil {
		err = res.Error
		return
	}
	var BomLines = make([]BomLine, 0)
	var WorkCenters = make([]WorkCenter, 0)
	var ByProducts = make([]ByProduct, 0)
	for _, vv := range data.BomLines {
		BomLines = append(BomLines, BomLine{
			Id:          vv.Id,
			ModelName:   "mrp.bom.line",
			ProductId:   vv.ProductId,
			ProductName: vv.ProductTmpl.Name,
			ProductQty:  vv.ProductQty,
		})
	}
	for _, vv := range data.Workcenters {
		WorkCenters = append(WorkCenters, WorkCenter{
			Id:              vv.Id,
			Name:            vv.Name,
			ModelName:       "mrp.routing.workcenter",
			WorkCenter:      vv.Workcenter.Name,
			TimeMode:        vv.TimeMode,
			TimeCycleManual: vv.TimeCycleManual,
			CompanyId:       data.CompanyId,
			CompanyName:     data.Company.Name,
		})
	}
	//副产品
	for _, vv := range data.ByProducts {
		ByProducts = append(ByProducts, ByProduct{
			Id:          vv.Id,
			ProductId:   vv.ProductId,
			ModelName:   "mrp.bom.byproduct",
			ProductName: vv.Product.ProductTmpl.Name,
			OperationId: vv.OperationId,
			Operation:   vv.Operation.Name,
			ProductQty:  vv.ProductQty,
		})
	}
	result.Id = data.Id
	result.ModelName = "mrp.bom"
	result.ProductId = data.ProductId
	result.ProductName = data.Product.ProductTmpl.Name
	result.ProductTmplName = data.ProductTmpl.Name
	result.ProductQty = data.ProductQty
	result.Code = data.Code
	result.Type = data.Type
	result.CompanyName = data.Company.Name
	result.CompanyId = data.CompanyId
	result.BomLine = BomLines
	result.WorkCenter = WorkCenters
	result.ByProduct = ByProducts
	result.Other = BomOther{
		data.ReadyToProduce,
		data.Consumption,
	}
	return
}

// WorkCenter 作业
type WorkCenter struct {
	Id              int64   `json:"id"`
	Name            string  `json:"name"`
	ModelName       string  `json:"model_name"`
	WorkCenter      string  `json:"work_center"`       //工作中心
	TimeMode        string  `json:"time_mode"`         //时长计算
	TimeCycleManual float64 `json:"time_cycle_manual"` //时长(分钟)
	CompanyName     string  `json:"company_name"`
	CompanyId       int64   `json:"company_id"`
}

// BomLine 组件
type BomLine struct {
	Id          int64   `json:"id"`
	ModelName   string  `json:"model_name"`
	ProductId   int64   `json:"product_id"`
	ProductName string  `json:"product_name"`
	ProductQty  float64 `json:"product_qty"` //数量
}

type BomItem struct {
	Id              int64        `json:"id"`
	ModelName       string       `json:"model_name"`
	ProductId       int64        `json:"product_id"`        //产品变体
	ProductName     string       `json:"product_name"`      //产品变体
	ProductTmplId   int64        `json:"product_tmpl_id"`   //产品
	ProductTmplName string       `json:"product_tmpl_name"` //产品
	ProductQty      float64      `json:"product_qty"`       //数量
	Code            string       `json:"code"`              //参考
	Type            string       `json:"type"`              //类型
	CompanyId       int64        `json:"company_id"`
	CompanyName     string       `json:"company_name"`
	WorkCenter      []WorkCenter `json:"work_center"` //作业
	BomLine         []BomLine    `json:"bom_line"`    //组件
	ByProduct       []ByProduct  `json:"by_product"`  //副产品
	Other           BomOther     `json:"other"`       //杂项
}

// BomOther 杂项
type BomOther struct {
	ReadyToProduce string `json:"ready_to_produce"`
	Consumption    string `json:"consumption"`
}

// ByProduct 副产品
type ByProduct struct {
	Id          int64   `json:"id"`
	ModelName   string  `json:"model_name"`
	ProductId   int64   `json:"product_id"`
	ProductName string  `json:"product_name"`
	OperationId int64   `json:"operation_id"`
	Operation   string  `json:"operation"`
	ProductQty  float64 `json:"product_qty"`
}
